package com.alex.wechat.mp.tag.service.impl;

import java.util.List;
import java.util.UUID;

import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.oConvertUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alex.wechat.mp.tag.entity.WxTagEntity;
import com.alex.wechat.mp.tag.entity.WxUserTagEntity;
import com.alex.wechat.mp.tag.service.WxTagServiceI;

@Service("wxTagService")
@Transactional
public class WxTagServiceImpl extends CommonServiceImpl implements WxTagServiceI {

  public <T> void delete(T entity) {
    super.delete(entity);
    // 执行删除操作配置的sql增强
    this.doDelSql((WxTagEntity) entity);
  }

  public void addMain(WxTagEntity wxTag, List<WxUserTagEntity> wxUserTagList) {
    wxTag.setUserCount(wxUserTagList.size());

    // 保存主信息
    this.save(wxTag);

    /** 保存-用户标签 */
    for (WxUserTagEntity wxUserTag : wxUserTagList) {
      // 外键设置
      wxUserTag.setTagId(wxTag.getId());
      this.save(wxUserTag);
    }
    // 执行新增操作配置的sql增强
    this.doAddSql(wxTag);
  }

  public void updateMain(WxTagEntity wxTag, List<WxUserTagEntity> wxUserTagList) {
    wxTag.setUserCount(wxUserTagList.size());

    // 保存主表信息
    this.saveOrUpdate(wxTag);
    // ===================================================================================
    // 获取参数
    Object id0 = wxTag.getId();
    // ===================================================================================
    // 1.查询出数据库的明细数据-用户标签
    String hql0 = "from WxUserTagEntity where 1 = 1 AND tAG_ID = ? ";
    List<WxUserTagEntity> wxUserTagOldList = this.findByHql(hql0, id0);
    // 2.筛选更新明细数据-用户标签
    if (wxUserTagList != null && wxUserTagList.size() > 0) {
      for (WxUserTagEntity oldE : wxUserTagOldList) {
        boolean isUpdate = false;
        for (WxUserTagEntity sendE : wxUserTagList) {
          // 需要更新的明细数据-用户标签
          if (oldE.getId().equals(sendE.getId())) {
            try {
              MyBeanUtils.copyBeanNotNull2Bean(sendE, oldE);
              this.saveOrUpdate(oldE);
            } catch (Exception e) {
              e.printStackTrace();
              throw new BusinessException(e.getMessage());
            }
            isUpdate = true;
            break;
          }
        }
        if (!isUpdate) {
          // 如果数据库存在的明细，前台没有传递过来则是删除-用户标签
          super.delete(oldE);
        }

      }
      // 3.持久化新增的数据-用户标签
      for (WxUserTagEntity wxUserTag : wxUserTagList) {
        if (oConvertUtils.isEmpty(wxUserTag.getId())) {
          // 外键设置
          wxUserTag.setTagId(wxTag.getId());
          this.save(wxUserTag);
        }
      }
    }
    // 执行更新操作配置的sql增强
    this.doUpdateSql(wxTag);
  }

  public void delMain(WxTagEntity wxTag) {
    // 删除主表信息
    this.delete(wxTag);
    // ===================================================================================
    // 获取参数
    Object id0 = wxTag.getId();
    // ===================================================================================
    // 删除-用户标签
    String hql0 = "from WxUserTagEntity where 1 = 1 AND tAG_ID = ? ";
    List<WxUserTagEntity> wxUserTagOldList = this.findByHql(hql0, id0);
    this.deleteAllEntitie(wxUserTagOldList);
  }

  /**
   * 默认按钮-sql增强-新增操作
   * 
   * @param id
   * @return
   */
  public boolean doAddSql(WxTagEntity t) {
    return true;
  }

  /**
   * 默认按钮-sql增强-更新操作
   * 
   * @param id
   * @return
   */
  public boolean doUpdateSql(WxTagEntity t) {
    return true;
  }

  /**
   * 默认按钮-sql增强-删除操作
   * 
   * @param id
   * @return
   */
  public boolean doDelSql(WxTagEntity t) {
    return true;
  }

  /**
   * 替换sql中的变量
   * 
   * @param sql
   * @return
   */
  public String replaceVal(String sql, WxTagEntity t) {
    sql = sql.replace("#{id}", String.valueOf(t.getId()));
    sql = sql.replace("#{create_name}", String.valueOf(t.getCreateName()));
    sql = sql.replace("#{create_by}", String.valueOf(t.getCreateBy()));
    sql = sql.replace("#{create_date}", String.valueOf(t.getCreateDate()));
    sql = sql.replace("#{update_name}", String.valueOf(t.getUpdateName()));
    sql = sql.replace("#{update_by}", String.valueOf(t.getUpdateBy()));
    sql = sql.replace("#{update_date}", String.valueOf(t.getUpdateDate()));
    sql = sql.replace("#{sys_org_code}", String.valueOf(t.getSysOrgCode()));
    sql = sql.replace("#{sys_company_code}", String.valueOf(t.getSysCompanyCode()));
    sql = sql.replace("#{account_id}", String.valueOf(t.getAccountId()));
    sql = sql.replace("#{name}", String.valueOf(t.getName()));
    sql = sql.replace("#{tag_id}", String.valueOf(t.getTagId()));
    sql = sql.replace("#{user_count}", String.valueOf(t.getUserCount()));
    sql = sql.replace("#{remark}", String.valueOf(t.getRemark()));
    sql = sql.replace("#{UUID}", UUID.randomUUID().toString());
    return sql;
  }
}